<template>
  <div class="box">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <div
        v-for="item in list"
        :key="item.id"
        @click="toTopic('/topicdetail', item.id)"
      >
        <img :src="item.scene_pic_url" alt="" />
        <p class="p1">{{ item.title }}</p>
        <p class="p2">{{ item.subtitle }}</p>
        <p class="p3">{{ item.price_info }}元起</p>
      </div>
    </van-list>
  </div>
</template>

<script>
import { listaction } from "@/api/topic";
export default {
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      start: 1,
    };
  },
  created() {
    // listaction({
    //   page:1
    // }).then(res =>{
    //   console.log(res);
    // })
  },
  methods: {
    toTopic(to, id) {
      this.$router.push({ path: to, query: { id: id } });
    },
    onLoad() {
      listaction({
        page: this.start,
      }).then((res) => {
        console.log(res);
        this.list.push(...res.data);
        this.loading = false;

        if (this.start == res.total) {
          this.finished = true;
        }

        this.start++;
      });
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      // setTimeout(() => {
      //   for (let i = 0; i < 10; i++) {
      //     this.list.push(this.list.length + 1);
      //   }

      //   // 加载状态结束
      //   this.loading = false;

      //   // 数据全部加载完成
      //   if (this.list.length >= 40) {
      //     this.finished = true;
      //   }
      // }, 1000);
    },
  },
};
</script>

<style lang="scss" scoped>
img {
  width: 100%;
}
p {
  margin-top: 10px;
  margin-bottom: 15px;
}
.p1 {
  margin-top: 15px;
  font-size: 18px;
}
.p2 {
  margin-bottom: 10px;
  font-size: 12px;
  color: #999;
}
.p3 {
  color: #f00;
}
.box {
  margin-bottom: 50px;
}
</style>